<template>
	<view>
		<!-- 轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" autoplay circular>
			<swiper-item v-for="item in swiperList" :key='item.goods_id'>
				<navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id">
					<image :src="item.image_src" mode="aspectFill"></image>
				</navigator>
			</swiper-item>
		</swiper>

		<!-- 分类导航 -->
		<view class="nav">
			<view class="nav-item" v-for="(item,index) in navList" :key='index' @click="goCates(item)">
				<image :src="item.image_src" mode="aspectFit"></image>
			</view>
		</view>

		<!-- 楼层区域 -->
		<view class="floor">
			<view class="floor-item" v-for="(item,index) in floorList" :key='index'>
				<!-- 楼层标题 -->
				<image :src="item.floor_title.image_src" class="floor-title"></image>
				<!-- 楼层图片 -->
				<view class="floor-item-box">
					<image :src="item2.image_src" v-for="(item2,index2) in item.product_list" :key='index2' class="floor-item-img"
					 :style="{width: item2.image_width+'rpx'}" @click="goGoodsList(item2.url)"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 轮播图数据
				swiperList: [],
				// 分类导航数据
				navList: [],
				// 楼层数据
				floorList: []
			};
		},
		onLoad() {
			this.getSwiperList()
			this.getNavList()
			this.getFloorList()
		},
		methods: {
			// 获取轮播图数据
			async getSwiperList() {
				const {
					data: res
				} = await uni.$req.get('/home/swiperdata')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.swiperList = res.message
			},

			// 获取分类导航数据
			async getNavList() {
				const {
					data: res
				} = await uni.$req.get('/home/catitems')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.navList = res.message
			},

			//  分类项被点击时候的事件处理函数
			goCates(val) {
				if (val.name === '分类') {
					// uni.switchTab({
					// 	url:'/pages/cates/cates'
					// })
					uni.switchTab({
						url: '/pages/cates/cates'
					})
				}
			},

			// 获取楼层数据
			async getFloorList() {
				const {
					data: res
				} = await uni.$req.get('/home/floordata')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.floorList = res.message
				this.floorList.forEach(x => {
					x.product_list.forEach(y => {
						y.url=y.navigator_url.replace(/.*\?/,'/subpkg/goods_list/goods_list?')
					})
				})
			},
			goGoodsList(url){
				console.log(url)
				uni.navigateTo({
					url
				})
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 340rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	.nav {
		display: flex;
		justify-content: space-around;
		margin: 10px 0;

		.nav-item {
			image {
				width: 128rpx;
				height: 140rpx;
			}
		}
	}

	.floor {
		.floor-title {
			height: 60rpx;
			width: 750rpx;
		}

		.floor-item-img {
			margin: 0 0 10rpx 10rpx;
			float: left;
			height: 188rpx;

			&:first-child {
				height: 386rpx;
			}
		}
	}
</style>
